package cn.com.zetatech.maple.ftp;

import cn.com.zetatech.maple.entity.FtpDFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class FTPUtil {

    public static FTPClient connect(String addr, int port, String username, String password, String controlEncoding) throws IOException {
        FTPClient client = new FTPClient();
        client.setControlEncoding(controlEncoding);
        client.connect(addr, port);
        username = StringUtils.isBlank(username) ? "Anonymous" : username;
        password = StringUtils.isBlank(password) ? "" : password;
        client.enterLocalPassiveMode();
        client.login(username, password);
        client.setFileType(2);
        client.setBufferSize(1024 * 1024);
        int reply = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            client.abort();
            client.disconnect();
        }
        return client;
    }

    public static void close(FTPClient client) throws IOException {
        if (client != null && client.isConnected()) {
            client.abort();
            client.disconnect();
        }
    }

    public static List<String> listFiles(FTPClient client, String remotePath, String dirPattern, String filePattern) {
        ArrayList files = new ArrayList();
        try {
            client.enterLocalPassiveMode();
            client.changeWorkingDirectory(remotePath);

            FTPFile[] ftpFiles = client.listFiles();
            if (ftpFiles != null && ftpFiles.length > 0) {
                FTPFile[] var7 = ftpFiles;
                int var8 = ftpFiles.length;

                for (int var9 = 0; var9 < var8; ++var9) {
                    FTPFile ftpFile = var7[var9];
                    String name = ftpFile.getName();
                    String fullPath = (remotePath + "/" + name).replaceAll("/+", "/");
                    if (ftpFile.isFile()) {
                        if (name.matches(filePattern)) {
                            files.add(fullPath);
                        }
                    } else if (name.matches(dirPattern)) {
                        if (!(".".equals(name) || "..".equals(name))) {
                            files.addAll(listFiles(client, fullPath, dirPattern, filePattern));
                        }
                    }
                }
            }
        } catch (IOException var13) {
            var13.printStackTrace();
        }

        return files;
    }

    public static List<FtpDFile> download(FTPClient client, String remotePath, String dirPattern, String filePattern, String localStoreDir) throws Exception {
        List<String> remotePaths = listFiles(client, remotePath, dirPattern, filePattern);
        List<FtpDFile> result = new ArrayList();

        String _remotePath;
        String localFullPath;
        boolean success;
        for (Iterator var7 = remotePaths.iterator(); var7.hasNext(); result.add(new FtpDFile(_remotePath, localFullPath, success))) {
            _remotePath = (String) var7.next();
            localFullPath = (localStoreDir + "/" + _remotePath).replaceAll("/+", "/");
            success = false;

            try {
                download(client, _remotePath, localFullPath);
                success = true;
            } catch (Exception var12) {
                var12.printStackTrace();
            }
        }

        return result;
    }

    public static String download(FTPClient client, String remotePath, String localFullPath) throws Exception {
        File localFile = new File(localFullPath);
        if (localFile.exists()) {
            localFile.delete();
        } else {
            File parent = localFile.getParentFile();
            if (!parent.exists() || !parent.isDirectory()) {
                parent.mkdirs();
            }
        }

        FileOutputStream outputStream = new FileOutputStream(localFile);

        String var5;
        try {
            client.retrieveFile(remotePath, outputStream);
            outputStream.flush();
            var5 = localFile.getAbsolutePath();
        } finally {
            if (Collections.singletonList(outputStream).get(0) != null) {
                outputStream.close();
            }
        }

        return var5;
    }
}